package com.zhaoyang.servlet;

import com.zhaoyang.dao.CourseDao;
import com.zhaoyang.dao.SelectedCourseDao;
import com.zhaoyang.model.Page;
import com.zhaoyang.model.SelectedCourse;
import com.zhaoyang.model.Student;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 赵扬
 * @time 2020/4/28 12:59
 */
@WebServlet("/selectedCourseServlet")
public class SelectedCourseServlet extends HttpServlet {
    private static final long serialVersionUID = 7120913402001186955L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getParameter("method");

        if ("toSelectedCourseListView".equals(method)) {
            try {
                request.getRequestDispatcher("view/selectedCourseList.jsp").forward(request, response);
            } catch (ServletException e) {
                e.printStackTrace();
            }
        } else if ("AddSelectedCourse".equals(method)) {
            addSelectedCourse(request, response);
        } else if ("SelectedCourseList".equals(method)) {
            getSelectedCourseList(request, response);
        } else if ("DeleteSelectedCourse".equals(method)) {
            deleteSelectedCourse(request, response);
        }
    }

    private void deleteSelectedCourse(HttpServletRequest request,
                                      HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        SelectedCourseDao selectedCourseDao = new SelectedCourseDao();
        SelectedCourse selectedCourse = selectedCourseDao.getSelectedCourse(id);
        String msg = "success";
        if (selectedCourse == null) {
            msg = "not found";
            response.getWriter().write(msg);
            return;
        }
        if (selectedCourseDao.deleteSelectedCourse(selectedCourse.getId())) {
            CourseDao courseDao = new CourseDao();
            courseDao.updateCourseSelectedNum(selectedCourse.getCourseId(), -1);
        } else {
            msg = "error";
        }
        response.getWriter().write(msg);
    }

    private void addSelectedCourse(HttpServletRequest request,
                                   HttpServletResponse response) throws IOException {
        int studentId = request.getParameter("studentid") == null ? 0 : Integer.parseInt(request.getParameter("studentid").toString());
        int courseId = request.getParameter("courseid") == null ? 0 : Integer.parseInt(request.getParameter("courseid").toString());
        CourseDao courseDao = new CourseDao();
        String msg = "success";
        if (courseDao.isFull(courseId)) {
            msg = "courseFull";
            response.getWriter().write(msg);
            return;
        }
        SelectedCourseDao selectedCourseDao = new SelectedCourseDao();
        if (selectedCourseDao.isSelected(studentId, courseId)) {
            msg = "courseSelected";
            response.getWriter().write(msg);
            return;
        }
        SelectedCourse selectedCourse = new SelectedCourse();
        selectedCourse.setStudentId(studentId);
        selectedCourse.setCourseId(courseId);
        if (selectedCourseDao.addSelectedCourse(selectedCourse)) {
            msg = "success";
        }
        courseDao.updateCourseSelectedNum(courseId, 1);
        response.getWriter().write(msg);
    }

    private void getSelectedCourseList(HttpServletRequest request,
                                       HttpServletResponse response) {
        int studentId = request.getParameter("studentid") == null ? 0 : Integer.parseInt(request.getParameter("studentid").toString());
        int courseId = request.getParameter("courseid") == null ? 0 : Integer.parseInt(request.getParameter("courseid").toString());
        Integer currentPage = request.getParameter("page") == null ? 1 : Integer.parseInt(request.getParameter("page"));
        Integer pageSize = request.getParameter("rows") == null ? 999 : Integer.parseInt(request.getParameter("rows"));
        SelectedCourse selectedCourse = new SelectedCourse();

        int userType = Integer.parseInt(request.getSession().getAttribute("userType").toString());
        if (userType == 2) {
            Student currentUser = (Student) request.getSession().getAttribute("user");
            studentId = currentUser.getId();
        }

        selectedCourse.setCourseId(courseId);
        selectedCourse.setStudentId(studentId);

        SelectedCourseDao selectedCourseDao = new SelectedCourseDao();

        List<SelectedCourse> courseList = selectedCourseDao.getSelectedCourseList(selectedCourse,
                new Page(currentPage, pageSize));
        long total = selectedCourseDao.getSelectedCourseListTotal(selectedCourse);
        response.setContentType("application/json;charset=UTF-8");
        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("total", total);
        ret.put("rows", courseList);
        try {
            String from = request.getParameter("from");
            if ("combox".equals(from)) {
                response.getWriter().write(JSONArray.fromObject(courseList).toString());
            } else {
                response.getWriter().write(JSONObject.fromObject(ret).toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
